package com.dwd.pet.service.impl;

import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.basic.utils.DistanceUtil;
import com.dwd.basic.utils.Point;
import com.dwd.basic.utils.SystemContext;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.org.pojo.Shop;
import com.dwd.pet.mapper.SearchMasterMsgMapper;
import com.dwd.pet.pojo.SearchMasterMsg;
import com.dwd.pet.service.ISearchMasterMsgService;
import com.dwd.user.mapper.UserMapper;
import com.dwd.user.pojo.LoginInfo;
import com.dwd.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/24 12:36
 * @Version 1.0
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 生成寻主信息
     * @param searchMasterMsg
     * @param httpRequest
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest httpRequest) {
        //得到是前台门户用户的登陆信息
        LoginInfo loginInfo = SystemContext.getLoginInfo(httpRequest);
        //根据logininfo查询对应得user
        //断言不为null
        assert loginInfo != null;
        User user = userMapper.queryById(loginInfo.getId());
        searchMasterMsg.setUser(user);
        //得到地址得经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //查询所有的店铺
        List<Shop> shops = shopMapper.queryAll();
        //查询最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        //设置店铺
        searchMasterMsg.setShop(nearestShop);
        //存储
        searchMasterMsgMapper.save(searchMasterMsg);
    }
}
